package com.app114.app.android.rk;

import java.util.List;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ShareCompat;
import br.com.dina.ui.widget.UITableView;
import br.com.dina.ui.widget.UITableView.ClickListener;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.app114.app.android.rk.bean.WebSiteItem;
import com.app114.app.android.rk.conf.Conf;
import com.app114.app.android.rk.utils.DataFactory;

public class CategoryListActivity extends BaseFragmentActivity implements ClickListener {

	// private GridView gridView;
	private UITableView mTableView;
	private String title;
	private List<WebSiteItem> mWebSiteItems;

	// @Override
	// public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
	// Intent intent = new Intent(mContext, Browser.class);
	// intent.setAction(Intent.ACTION_VIEW);
	// intent.setData(Uri.parse(mWebSiteItems.get(arg2).getGUrl()));
	// startActivity(intent);
	// }

	@Override
	protected void activitySetup(Bundle savedInstanceState) {
		Intent intent = getIntent();
		if (intent == null || (title = intent.getAction()) == null) {
			return;
		}

		setContentView(R.layout.activity_catagory_list);
		getSupportActionBar().setTitle(title);

		mTableView = (UITableView) findViewById(R.id.tableView);
		createList();
		mTableView.commit();

		// gridView = (GridView) findViewById(R.id.gridView);
		// gridView.setOnItemClickListener(this);
		// gridView.setAdapter(new WebsAdapter(mContext, mWebSiteItems));
	}

	private void createList() {
		mTableView.setClickListener(this);
		mWebSiteItems = DataFactory.getWebSiteList(title);
		for (WebSiteItem ws : mWebSiteItems) {
			mTableView.addBasicItem(ws.getName());
		}

	}

	@Override
	protected boolean _onCreateOptionsMenu(Menu menu) {
		getSupportMenuInflater().inflate(R.menu.activity_share, menu);
		return true;
	}

	@Override
	protected boolean _onOptionsItemSelected(MenuItem item) {
		ShareCompat.IntentBuilder.from(this).setType("text/plain").setText(DataFactory.SHARE_INFO + "\r\n" + Conf.URL_APP)
		.startChooser();
		return true;
	}

	@Override
	protected void activityDestory() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onClick(int index) {
		Intent intent = new Intent(mContext, Browser.class);
		intent.setAction(Intent.ACTION_VIEW);
		intent.setData(Uri.parse(mWebSiteItems.get(index).getGUrl()));
		intent.putExtra(Conf.KEY_WEB_NAME, mWebSiteItems.get(index).getName()) ;
		startActivity(intent);
	}
}
